package com.zzg.srb.core.controller.api;


import com.zzg.srb.base.utils.JwtUtils;
import com.zzg.srb.common.exception.Assert;
import com.zzg.srb.common.result.R;
import com.zzg.srb.common.result.ResponseEnum;
import com.zzg.srb.common.utils.RegexValidateUtils;
import com.zzg.srb.core.entity.pojo.UserInfo;
import com.zzg.srb.core.entity.vo.LoginVo;
import com.zzg.srb.core.entity.vo.RegisterVo;
import com.zzg.srb.core.entity.vo.UserIndexVo;
import com.zzg.srb.core.entity.vo.UserInfoVo;
import com.zzg.srb.core.service.UserInfoService;
import com.zzg.srb.core.utils.CurrentLoginUserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author zhengzg
 * @since 2022-05-27
 */
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
public class UserInfoController {

    private Logger log = LoggerFactory.getLogger(UserInfoController.class);

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("会员注册")
    @PutMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        log.info("会员接口 ====> UserInfoController.register: " + registerVo);

        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();
        String password = registerVo.getPassword();

        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        Assert.notEmpty(password,ResponseEnum.PASSWORD_NULL_ERROR);
        Assert.notEmpty(code,ResponseEnum.CODE_NULL_ERROR);

        String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:code:" + mobile);

        Assert.equals(code,codeGen,ResponseEnum.CODE_ERROR);

        //注册
        userInfoService.register(registerVo);
        return R.ok().message("注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R<UserInfoVo> login(@RequestBody LoginVo loginVo, HttpServletRequest request){
        log.info("会员接口 ====> UserInfoController.login: " + loginVo);

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        Assert.notEmpty(password,ResponseEnum.PASSWORD_NULL_ERROR);

        String ip = request.getRemoteAddr();
        return R.ok().message("登录成功").data(userInfoService.login(loginVo,ip));
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        boolean checkToken = JwtUtils.checkToken(token);
        return checkToken ? R.ok() : R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
    }

    @ApiOperation("锁定和解锁")
    @ApiImplicitParam(name = "userInfo",value = "用户信息",dataType = "UserInfo",required = true)
    @PostMapping("/lock")
    public R lock(@RequestBody UserInfo userInfo){
        Integer lock = userInfoService.lock(userInfo);
        return lock > 0 ? R.ok().message(userInfo.getStatus() == 1 ? "解锁成功" : "锁定成功") : R.ok().message("用户状态修改失败");
    }

    @ApiOperation("校验手机号是否合法")
    @ApiImplicitParam(name = "mobile")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable String mobile){
        return userInfoService.checkMobile(mobile);
    }

    @ApiOperation("获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public R getIndexUserInfo() {
        Long userId = CurrentLoginUserUtils.getLoginUser();
        UserIndexVo userIndexVO = userInfoService.getIndexUserInfo(userId);
        return R.ok().data(userIndexVO);
    }
}

